<?php

$ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);

//监听WebSocket连接打开事件i  当有客户端连接上的时候 触发
$ws->on('open', function ($ws, $request) {
    $redis = new Redis();
	$redis->connect('127.0.0.1',6379);
	$redis->sadd('ws_set',$request->fd);
});

//监听WebSocket消息事件 当客户端给服务器端发送消息的时候 触发
$ws->on('message', function ($ws, $frame) {
   $redis = new Redis();
   $redis->connect('127.0.0.1',6379);
   $fds = $redis->smembers('ws_set');
   foreach($fds as $fd){
	   if( $fd == $frame->fd ) continue;
	   $ws->push($fd, $frame->data); // 服务端给客户端推送消息
   }
   
});


$ws->start();
